﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;
using PBDesk.Business.UserManager;
using System.Web.Security;
using PBDesk.Utilities;

namespace PBDesk.Web.Main.Controllers
{
    public class LogonController :  Controller
    {
        public ActionResult Index()
        {
            bool success = false;
            string returnUrl = Request.QueryString["ReturnUrl"];
            string redirectUrl = @"/UI/Gen/Default.aspx";
            if (Request.IsAuthenticated)
            {
                if (redirectUrl.IsNotNullAndNotEmpty())
                {
                    redirectUrl = returnUrl;
                }
            }
            else
            {
                if (Request.QueryString != null && Request.QueryString.Count > 0)
                {
                    NameValueCollection qs = Request.QueryString;
                    LogonStatus status = LogonHelper.LogonUser(qs);
                    switch (status)
                    {
                        case LogonStatus.Success:
                            {
                                success = true;
                                break;
                            }
                        case LogonStatus.Failure:
                            {
                                success = false;
                                break;
                            }
                        case LogonStatus.NewUser:
                            {
                                return new PermanentRedirectResult("/UI/Gen/Signup.aspx", qs);
                                break;
                            }
                    }
                    if (status == LogonStatus.Success)
                    {
                        
                    }
                }
            }
            Response.StatusCode = 301;
            if (success == true)
            {
                
                if (redirectUrl.IsNotNullAndNotEmpty())
                {
                    FormsAuthentication.RedirectFromLoginPage("test", false);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie("test", false);
                    Response.RedirectLocation = redirectUrl;
                }
                               
            }
            else
            {
                //Response.RedirectLocation = @"/UI/Gen/Default.aspx?error"; 
                FormsAuthentication.RedirectToLoginPage();
            }
            return new ContentResult();
        }
    }

    public class PermanentRedirectResult : ActionResult
    {
        public string Url { get; set; }
        public NameValueCollection QueryString { get; set; }

        public PermanentRedirectResult(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException("url is null or empty", "url");
            }
            this.Url = url;
        }

        public PermanentRedirectResult(string url, NameValueCollection queryString)
        { 
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException("url is null or empty", "url");
            }
            this.Url = url;
            if (queryString != null && queryString.Count > 0)
            {
                this.Url += queryString.ToQueryString();
            }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            context.HttpContext.Response.StatusCode = 301;
            context.HttpContext.Response.RedirectLocation = Url;
            context.HttpContext.Response.End();
        }
    }
}